home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / Gfx / Edit / TSMorph / src / saveilbm.c < prev    next >
C/C++ Source or Header  |  1994-10-30  |  5KB  |  176 lines

  1. /* saveilbm.c 04/92  C. Scheppner CBM
  2.  *
  3.  * High-level ILBM save routines
  4.  */
  5.  
  6. // Minor change to error handling MJP
  7.  
  8. #define INTUI_V36_NAMES_ONLY
  9.  
  10. #include "iffp/ilbm.h"
  11. #include "iffp/ilbmapp.h"
  12.  
  13. extern struct Library *GfxBase;
  14.  
  15. /* screensave.c
  16.  *
  17.  * Given an ILBMInfo with a  currently available (not in use)
  18.  *   ParseInfo->iff IFFHandle, a screen pointer, filename, and
  19.  *   optional chunklist, will save screen as an ILBM
  20.  * The struct Chunk *chunklist1 and 2 are for chunks you wish written
  21.  * out other than BMHD, CMAP, and CAMG (they will be screened out
  22.  * because they are computed and written separately).
  23.  *
  24.  * Note -  screensave passes NULL for transparent color and mask
  25.  *
  26.  * Returns 0 for success or an IFFERR (libraries/iffparse.h)
  27.  */
  28. /* MJP
  29. LONG screensave(struct ILBMInfo *ilbm,
  30.             struct Screen *scr,
  31.             struct Chunk *chunklist1, struct Chunk *chunklist2,
  32.             UBYTE *filename)
  33. {
  34. extern struct Library *GfxBase;
  35. UWORD *colortable, count;
  36. ULONG modeid;
  37. LONG error;
  38. int k;
  39.  
  40.     if(GfxBase->lib_Version >= 36)
  41.     modeid=GetVPModeID(&scr->ViewPort);
  42.     else
  43.     modeid = scr->ViewPort.Modes & OLDCAMGMASK;
  44.  
  45.     count = scr->ViewPort.ColorMap->Count;
  46.     if(colortable = (UWORD *)AllocMem(count << 1, MEMF_CLEAR))
  47.     {
  48.     for(k=0; k<count; k++)    colortable[k]=GetRGB4(scr->ViewPort.ColorMap,k);
  49.  
  50.         error = saveilbm(ilbm, &scr->BitMap, modeid,
  51.         scr->Width, scr->Height, scr->Width, scr->Height,
  52.         colortable, count, 4,
  53.         mskNone, 0,
  54.         chunklist1, chunklist2, filename);
  55.     FreeMem(colortable,count << 1);
  56.     }
  57.     else error = IFFERR_NOMEM;
  58.     return(error);
  59. }
  60. */
  61.  
  62. /* saveilbm
  63.  *
  64.  * Given an ILBMInfo with a currently available (not-in-use)
  65.  *   ParseInfo->iff IFFHandle, a BitMap ptr,
  66.  *   modeid, widths/heights, colortable, ncolors, bitspergun,
  67.  *   masking, transparent color, optional chunklists, and filename,
  68.  *   will save the bitmap as an ILBM.
  69.  *
  70.  *  if bitspergun=4,  colortable is words, each with nibbles 0RGB
  71.  *  if bitspergun=8,  colortable is byte guns of RGBRGB etc. (like a CMAP)
  72.  *  if bitspergun=32, colortable is ULONG guns of RGBRGB etc.
  73.  *     Only the high eight bits of each gun will be written to CMAP.
  74.  *     Four bit guns n will be saved as nn
  75.  *
  76.  * The struct Chunk *chunklist is for chunks you wish written
  77.  * other than BMHD, CMAP, and CAMG (they will be screened out)
  78.  * because they are calculated and written separately
  79.  *
  80.  * Returns 0 for success, or an IFFERR
  81.  */
  82. LONG saveilbm(struct ILBMInfo *ilbm,
  83.         struct BitMap *bitmap, ULONG modeid,
  84.         WORD width, WORD height, WORD pagewidth, WORD pageheight,
  85.         APTR colortable, UWORD ncolors, UWORD bitspergun,
  86.         WORD masking, WORD transparentColor,
  87.         struct Chunk *chunklist1, struct Chunk *chunklist2,
  88.         UBYTE *filename)
  89. {
  90. struct IFFHandle *iff;
  91. UBYTE *bodybuf;         // MJP
  92. LONG error = 0L;
  93. #define BODYBUFSZ    4096
  94.  
  95.     iff = ilbm->ParseInfo.iff;
  96.  
  97.     if(!(modeid & 0xFFFF0000))    modeid &= OLDCAMGMASK;
  98.  
  99.     if(!(bodybuf = AllocMem(BODYBUFSZ,MEMF_PUBLIC)))
  100.     {
  101.     message(SI(MSG_IFFP_NOMEM),NULL,9);        // MJP 9 = HE_IFFMEM
  102.     return(IFFERR_NOMEM);
  103.     }
  104.  
  105.     if(!(error = openifile(ilbm, filename, IFFF_WRITE)))
  106.     {
  107.     D(bug("Opened %s for write\n",filename));
  108.  
  109.     error = PushChunk(iff, ID_ILBM, ID_FORM, IFFSIZE_UNKNOWN);
  110.  
  111.     D(bug("After PushChunk FORM ILBM - error = %ld\n", error));
  112.  
  113.         initbmhd(&ilbm->Bmhd, bitmap, masking, cmpByteRun1, transparentColor,
  114.                     width, height, pagewidth, pageheight, modeid);
  115.  
  116.     D(bug("Error before putbmhd = %ld\n",error));
  117.  
  118.     CkErr(putbmhd(iff,&ilbm->Bmhd));    
  119.  
  120.     if(colortable)    CkErr(putcmap(iff,colortable,ncolors,bitspergun));
  121.  
  122.     ilbm->camg = modeid;
  123.     D(bug("before putcamg - error = %ld\n",error));
  124.     if (colortable) {
  125.         CkErr(putcamg(iff,&modeid));    // MJP
  126.     }
  127.  
  128.     D(bug("Past putBMHD, CMAP, CAMG - error = %ld\n",error));
  129.  
  130.     /* Write out chunklists 1 & 2 (if any), except for
  131.      * any BMHD, CMAP, or CAMG (computed/written separately)
  132.      */
  133. /* MJP
  134.     for(chunk = chunklist1; chunk; chunk = chunk->ch_Next)
  135.         {
  136.         D(bug("chunklist1 - have a %.4s\n",&chunk->ch_ID));
  137.         chunkID = chunk->ch_ID;
  138.         if((chunkID != ID_BMHD)&&(chunkID != ID_CMAP)&&(chunkID != ID_CAMG))
  139.         {
  140.         size = chunk->ch_Size==IFFSIZE_UNKNOWN ?
  141.             strlen(chunk->ch_Data) : chunk->ch_Size;
  142.         D(bug("Putting %.4s\n",&chunk->ch_ID));
  143.         CkErr(PutCk(iff, chunkID, size, chunk->ch_Data));
  144.         }
  145.         }
  146.  
  147.     for(chunk = chunklist2; chunk; chunk = chunk->ch_Next)
  148.         {
  149.         chunkID = chunk->ch_ID;
  150.         D(bug("chunklist2 - have a %.4s\n",&chunk->ch_ID));
  151.         if((chunkID != ID_BMHD)&&(chunkID != ID_CMAP)&&(chunkID != ID_CAMG))
  152.         {
  153.         size = chunk->ch_Size==IFFSIZE_UNKNOWN ?
  154.             strlen(chunk->ch_Data) : chunk->ch_Size;
  155.         D(bug("Putting %.4s\n",&chunk->ch_ID));
  156.         CkErr(PutCk(iff, chunkID, size, chunk->ch_Data));
  157.         }
  158.         }
  159. */
  160.     /* Write out the BODY
  161.      */
  162.     CkErr(putbody(iff, bitmap, NULL, &ilbm->Bmhd, bodybuf, BODYBUFSZ));
  163.  
  164.     D(bug("Past putbody - error = %ld\n",error));
  165.  
  166.  
  167.     CkErr(PopChunk(iff));    /* close out the FORM */
  168.     closeifile(ilbm);    /* and the file */
  169.     }
  170.  
  171.     FreeMem(bodybuf,BODYBUFSZ);
  172.  
  173.     return(error);
  174. }
  175.  
  176.